<template>
  <el-card>
    <el-row :gutter="20" class="header">
      <el-col :span="7">
        <el-input v-model="queryForm.name" placeholder="请输入姓名..." clearable />
      </el-col>
      <el-button type="primary" @click="initUserList">搜索</el-button>
    </el-row>
    <el-table :data="tableData" stripe style="width: 100%;text-align: center">
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="userName" label="请假人" width="100" />
      <el-table-column label="请假时间">
        <template v-slot="scope">
          {{ scope.row.startTime }} ~ {{ scope.row.endTime }}
        </template>
      </el-table-column>
      <el-table-column prop="reason" label="请假原因" />
      <el-table-column prop="picture" label="图片" width="100">
        <template v-slot="scope">
          <img :src="scope.row.picture" width="50" height="50">
        </template>
      </el-table-column>
      <el-table-column prop="teacherName" label="审批老师" width="100" />
      <el-table-column prop="status" label="状态" width="200">
        <template v-slot="scope">
          <el-tag v-if="scope.row.status == 0" type="warning">待审批</el-tag>
          <el-tag v-if="scope.row.status == 1" type="danger">已拒绝</el-tag>
          <el-tag v-if="scope.row.status == 2" type="success">已通过</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="300" align="center">
        <template v-slot="scope">
          <el-button
              v-if="(isAdmin || (isTeacher && scope.row.userId !== currentUser.id)) && (!scope.row.status || scope.row.status == 0)"
              type="success"
              size="small"
              icon="el-icon-edit-outline"
              @click="update(scope.row,1)"
            >通过</el-button>
            <el-button
              v-if="(isAdmin || (isTeacher && scope.row.userId !== currentUser.id)) && (!scope.row.status || scope.row.status == 0)"
              type="primary"
              size="small"
              icon="el-icon-edit-outline"
              @click="update(scope.row,2)"
            >拒绝</el-button>
            <el-button
              v-if="isAdmin || (isTeacher && scope.row.userId === currentUser.id)"
              type="danger"
              size="small"
              icon="el-icon-delete"
              @click="deleted(scope.row)"
            >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      :current-page="queryForm.pageNum"
      :page-sizes="[10, 20, 30, 40,50]"
      :page-size="queryForm.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

  </el-card>
</template>

<script>
import { getPleaseLeaveList, updateStatus, deletePleaseLeave, getPleaseLeave } from '@/api/table'
export default {
  data() {
    return {
      queryForm: {
        pageSize: 10,
        pageNum: 1,
        name: ''
      },
      tableData: [],
      total: 0
    }
  },
  created() {
    this.initUserList()
  },
  computed: {
    isAdmin() {
      const userInfo = JSON.parse(localStorage.getItem('userInfo'))
      return userInfo && userInfo.studentId === 'admin' && userInfo.identity === '1'
    },
    isTeacher() {
      const userInfo = JSON.parse(localStorage.getItem('userInfo'))
      return userInfo && userInfo.identity == 1 && userInfo.studentId !== 'admin'
    },
    isStudent() {
      const userInfo = JSON.parse(localStorage.getItem('userInfo'))
      return userInfo && userInfo.identity == 0
    },
    currentUser() {
      return JSON.parse(localStorage.getItem('userInfo')) || {}
    }
  },
  methods: {
    // initUserList() {
    //   const userInfo = this.currentUser
    //   if (this.isAdmin) {
    //     // 管理员查所有请假
    //     getPleaseLeaveList(this.queryForm).then(res => {
    //       if (res.code == 200) {
    //         this.tableData = res.data.records
    //         this.total = res.data.total
    //       }
    //     })
    //   } else if (this.isTeacher) {
    //     // 老师查自己和自己班级学生的请假
    //     getPleaseLeaveList({ ...this.queryForm, teacherId: userInfo.id }).then(res => {
    //       if (res.code == 200) {
    //         this.tableData = res.data.records
    //         this.total = res.data.total
    //       }
    //     })
    //   } else if (this.isStudent) {
    //     // 学生只能查自己的请假
    //     getPleaseLeaveList({ ...this.queryForm, userId: userInfo.id }).then(res => {
    //       if (res.code == 200) {
    //         this.tableData = res.data.records
    //         this.total = res.data.total
    //       }
    //     })
    //   }
    // },
    initUserList() {
      const userInfo = this.currentUser
      if (this.isAdmin) {
        // 管理员查所有请假
        getPleaseLeaveList(this.queryForm).then(res => {
          if (res.code == 200) {
            this.tableData = res.data.records
            this.total = res.data.total
          }
        })
      } else {
        // 老师和学生都用 getPleaseLeave，userId为当前用户id
        getPleaseLeave({ userId: userInfo.id }).then(res => {
          if (res.code == 200) {
            // 这里后端返回的是list，不是分页
            this.tableData = res.data
            this.total = res.data.length
          }
        })
      }
    },
    handleSizeChange(e) {
      console.log(e)
      this.queryForm.pageSize = e
      this.initUserList()
    },
    handleCurrentChange(e) {
      this.queryForm.pageNum = e
      this.initUserList()
    },
    update(item, index) {
      if (index == 1) {
        this.$confirm('此操作将通过此记录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          updateStatus({ leaveId: item.id,
            status: 2 }).then(res => {
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
              this.initUserList()
            }
          })
        })
      } else {
        this.$confirm('此操作将拒绝此纪录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          updateStatus({ leaveId: item.id,
            status: 1 }).then(res => {
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
              this.initUserList()
            }
          })
        })
      }
    },
    deleted(item) {
      this.$confirm('此操作将删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deletePleaseLeave({ id: item.id }).then(res => {
          if (res.code == 200) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.initUserList()
          }
        })
      })
    }
  }
}
</script>

<style scoped>

</style>
